﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Tzync
{
    public class ProfileSystem
    {
        private String cpuID;
        private List<String> profiles;

        public ProfileSystem(String cpuID)
        {
            this.cpuID = cpuID;
            profiles = new List<String>();
            ReadProfiles();
        }

        public String CpuID
        {
            get { return cpuID; }
            set { cpuID = value; }
        }

        public List<String> Profiles
        {
            get { return profiles; }
            set { profiles = value; }
        }

        public Profile CreateProfile(String profileName, String rootAddress)
        {
            foreach (String str in profiles)
            {
                if (str.Equals(profileName)) throw new ProfileNotUniqueException();
            }
            Profile newProfile = new Profile(cpuID, profileName, rootAddress);
            profiles.Add(profileName);            
            return newProfile;
        }

        public Profile ModifyProfile(Profile p, String rootAddress)
        {
            Profile newProfile = new Profile(cpuID, p.ProfileName, rootAddress);
            return newProfile;
        }

        public Profile GetProfile(String profileName)
        {
            Profile p = new Profile(cpuID, profileName);
            return p;
        }

        private void ReadProfiles()
        {
            TextReader tr;
            String input = "", programPath = "";
            FileStream fs;

            programPath = Directory.GetCurrentDirectory();
            //Creating the Tzync folder
            Directory.CreateDirectory(Path.Combine(programPath, @"Tzync"));
            programPath = Path.Combine(programPath, @"Tzync\SystemProfile");

            if (!File.Exists(programPath))
            {
                // create SystemProfile file for the very first time                
                fs = File.Create(programPath);
                fs.Close();
            }
            else if (File.Exists(programPath))
            {
                //Open file
                tr = new StreamReader(programPath);
                while ((input = tr.ReadLine()) != null)
                {
                    if (input.Trim().Length != 0)
                    {
                        profiles.Add(input);
                    }
                }
                tr.Close();
            }
        }

        public List<String> GetAvailableProfiles()
        {
            List<String> availableProfiles = new List<string>();
            Profile p;
            foreach (String str in profiles)
            {
                p = new Profile(cpuID, str);
                if (p.SystemName == SystemType.Host || p.SystemName == SystemType.Guest)
                    availableProfiles.Add(p.ProfileName);
                else if (!p.HasB)
                    availableProfiles.Add(p.ProfileName);
            }            
            
            return availableProfiles;
        }

        public void RemoveProfile(String ProfileName)
        {
            profiles.Remove(ProfileName);

            Profile P = new Profile(cpuID, ProfileName);
            DirectoryInfo D = new DirectoryInfo(P.ProfilePath);
            P.DeleteDirectory(D);
            Directory.Delete(P.ProfilePath, true);
        }

        public void WriteProfile()
        {
            StreamWriter SW;
            FileStream fs;
            List<String> data = new List<string>();

            String programPath = Path.Combine(Directory.GetCurrentDirectory(), @"Tzync\SystemProfile");

            File.Delete(programPath);
            fs = File.Create(programPath);
            fs.Close();

            SW = File.AppendText(programPath);
            foreach (String str in profiles)
            {
                SW.WriteLine(str);
            }
            SW.Close();
        }
    }
}
